// 实现消息转发子服务的服务器
#include "transmit_server.hpp"

DEFINE_bool(run_mode, false, "程序的运行模式，false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

// 命令行解析获取服务注册的参数
DEFINE_string(registry_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(instance_name, "/transmit_service/instance", "当前实例名称");
DEFINE_string(access_host, "127.0.0.1:10004", "当前实例的外部访问地址");

// 命令行解析获取RPC的参数
DEFINE_int32(rpc_listen_port, 10004, "Rpc服务器监听端口");
DEFINE_int32(rpc_timeout, -1, "Rpc调用超时时间");
DEFINE_int32(rpc_threads, 1, "Rpc的IO线程数量");

DEFINE_string(mq_user, "root", "rabbitmq访问用户名");
DEFINE_string(mq_password, "123456", "rabbitmq访问密码");
DEFINE_string(mq_host, "127.0.0.1:5672", "rabbitmq服务器地址信息 host:port");
DEFINE_string(mq_exchange_name, "mesg_exchange", "持久化消息发布交换机名称");
DEFINE_string(mq_queue_name, "mesg_queue", "持久化消息发布队列名称");
DEFINE_string(mq_binding_key, "routing_key", "绑定键");    // 这里我们默认设置为 和 rabbitmq default_routing_key中的一致！

// 关心的服务名称
DEFINE_string(care_service, "/service/user_service", "服务名称");

// mysql的参数
DEFINE_string(mysql_user_name, "root", "MySQL用户名称");
DEFINE_string(mysql_password, "Czh432248", "MySQL用户密码");
DEFINE_string(mysql_db_name, "ChatRoom", "MySQL数据库默认库名称");
DEFINE_string(mysql_host, "127.0.0.1", "这是MySQL服务器地址");
DEFINE_int32(mysql_port, 0, "这是MySQL服务器端口");
DEFINE_string(mysql_cest, "utf8", "MySQL客户端字符集");
DEFINE_int32(mysql_max_num, 4, "MySQL连接池最大连接数量");

int main(int argc, char *argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    bite_im::initLogger(FLAGS_run_mode, FLAGS_log_file, FLAGS_log_level);

    bite_im::MsgTransmitServerBuilder mtsb;

    // 获取rabbitmq客户端
    mtsb.make_mq_object(FLAGS_mq_user, FLAGS_mq_password, FLAGS_mq_host,
                        FLAGS_mq_exchange_name, FLAGS_mq_queue_name, FLAGS_mq_binding_key);
    // 获取mysql客户端
    mtsb.make_mysql_object(FLAGS_mysql_user_name, FLAGS_mysql_password, FLAGS_mysql_db_name,
                           FLAGS_mysql_host, FLAGS_mysql_port, FLAGS_mysql_cest, FLAGS_mysql_max_num);
    // 获取查找模块
    mtsb.make_find_object(FLAGS_registry_host, FLAGS_base_service, FLAGS_care_service);
    // 搭建rpc  注意rpc要在查找模块之后
    mtsb.make_rpc_server(FLAGS_rpc_listen_port, FLAGS_rpc_timeout, FLAGS_rpc_threads);
    // 获取注册模块
    mtsb.make_reg_object(FLAGS_registry_host, FLAGS_base_service + FLAGS_instance_name, FLAGS_access_host);
    auto server = mtsb.build();
    server->start();
    return 0;
}